package Clases;

import Clases.Estudiante;
import java.awt.Frame;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InscribirEstudiante.java
 *
 * Created on 16/02/2011, 11:55:23 AM
 */
/**
 *
 * @author Mariant
 */
public class InscribirEstudiante extends javax.swing.JFrame {

    /** Creates new form InscribirEstudiante */
    public InscribirEstudiante() {
        initComponents();

        ;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtnombre = new javax.swing.JTextField();
        txtapellido = new javax.swing.JTextField();
        txtcedula = new javax.swing.JTextField();
        btnguardar = new javax.swing.JButton();
        btncancelar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();
        btnbuscar = new javax.swing.JButton();
        btnretirar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Datos del Estudiante");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(100, 10, 160, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Nombre");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(50, 150, 50, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Apellido");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(50, 110, 50, 14);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Cedula");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(53, 70, 50, 14);
        getContentPane().add(txtnombre);
        txtnombre.setBounds(100, 150, 100, 30);
        getContentPane().add(txtapellido);
        txtapellido.setBounds(100, 110, 100, 30);
        getContentPane().add(txtcedula);
        txtcedula.setBounds(100, 70, 100, 30);

        btnguardar.setBackground(new java.awt.Color(0, 204, 204));
        btnguardar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnguardar.setText("Guardar");
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        getContentPane().add(btnguardar);
        btnguardar.setBounds(40, 230, 90, 30);

        btncancelar.setBackground(new java.awt.Color(0, 204, 204));
        btncancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btncancelar.setText("Cancelar");
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btncancelar);
        btncancelar.setBounds(140, 230, 90, 30);

        btnsalir.setBackground(new java.awt.Color(0, 204, 204));
        btnsalir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnsalir.setText("Salir");
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        getContentPane().add(btnsalir);
        btnsalir.setBounds(240, 230, 80, 30);

        btnbuscar.setBackground(new java.awt.Color(0, 204, 204));
        btnbuscar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnbuscar.setText("Buscar");
        btnbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbuscarActionPerformed(evt);
            }
        });
        getContentPane().add(btnbuscar);
        btnbuscar.setBounds(250, 70, 90, 40);

        btnretirar.setBackground(new java.awt.Color(0, 204, 204));
        btnretirar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnretirar.setText("Retirar");
        btnretirar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnretirarActionPerformed(evt);
            }
        });
        getContentPane().add(btnretirar);
        btnretirar.setBounds(250, 120, 90, 40);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-373)/2, (screenSize.height-319)/2, 373, 319);
    }// </editor-fold>//GEN-END:initComponents

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        // TODO add your handling code here:

        if ((txtnombre.getText().isEmpty()) || (txtapellido.getText().isEmpty()) || (txtcedula.getText().isEmpty())) // Mostrar mensaje
        {
            JOptionPane.showMessageDialog(new Frame(), "faltan campos, verifique", "validacion", JOptionPane.INFORMATION_MESSAGE);
        } else {
            try {
                int x = Integer.parseInt(txtcedula.getText());
                Estudiante estudiante = new Estudiante();
                estudiante.setNombre(txtnombre.getText());
                estudiante.setApellido(txtapellido.getText());
                estudiante.setCedula(txtcedula.getText());
                Menu.getSec().agregarEstudiante(estudiante);
                Menu.setVariableEstudiante(true);
                JOptionPane.showMessageDialog(new Frame(), "Se ha agregado con exito", "informacion", JOptionPane.INFORMATION_MESSAGE);
                txtapellido.setText("");
                txtnombre.setText("");
                txtcedula.setText("");
                estudiante = null;
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(new Frame(), "Error la cedula no es un numero ");

            }
        }

    }//GEN-LAST:event_btnguardarActionPerformed

    private void btnbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbuscarActionPerformed
        // TODO add your handling code here:
        if (!txtcedula.getText().isEmpty()) {
            int encontro = 0;
            txtapellido.setText("");
            txtnombre.setText("");
            encontro = Menu.getSec().buscarEstudiante(txtcedula.getText());

            if (encontro != -1) {
                txtapellido.setText(Menu.getSec().getEstudiante(encontro).getApellido());
                txtnombre.setText(Menu.getSec().getEstudiante(encontro).getNombre());
            } else {
                JOptionPane.showMessageDialog(new Frame(), "No se ha encontrado el alumno a buscar", "Informacion", JOptionPane.INFORMATION_MESSAGE);
            }


        } else {
            JOptionPane.showMessageDialog(new Frame(), "Faltan campos", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_btnbuscarActionPerformed

    private void btnretirarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnretirarActionPerformed
        // TODO add your handling code here:
        if (!txtcedula.getText().isEmpty()) {
            int encontro = 0;
            txtapellido.setText("");
            txtnombre.setText("");
            encontro = Menu.getSec().buscarEstudiante(txtcedula.getText());

            if (encontro != -1) {
                Menu.getSec().retirarEstudiante(txtcedula.getText());
                txtcedula.setText("");
                JOptionPane.showMessageDialog(new Frame(), "Se ha retirado el alumno con exito", "informacion", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(new Frame(), "No se ha encontrado el alumno a retirar", "Informacion", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(new Frame(), "Faltan campos", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }


    }//GEN-LAST:event_btnretirarActionPerformed

    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
        // TODO add your handling code here:
        txtcedula.setText("");
        txtapellido.setText("");
        txtnombre.setText("");
    }//GEN-LAST:event_btncancelarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnsalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InscribirEstudiante().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnbuscar;
    private javax.swing.JButton btncancelar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnretirar;
    private javax.swing.JButton btnsalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtcedula;
    private javax.swing.JTextField txtnombre;
    // End of variables declaration//GEN-END:variables
}
